package com.exm.collectcodenew.web.service.impl;

import com.exm.collectcodenew.web.service.UserEnumService;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class UserEnumServiceImpl implements UserEnumService {
    /**
     * 获取枚举类的所有值
     */

    @Override
    public <T extends Enum<T>> List<T> getAllEnums(Class<T> enumClass) {
        return Arrays.asList(enumClass.getEnumConstants());
    }
    /**
     * 获取枚举的code-desc映射
     */
    @Override
    public  <T extends Enum<T>> Map<Object, String> getCodeDescMap(Class<T> enumClass) {
        return Arrays.stream(enumClass.getEnumConstants())
                .collect(Collectors.toMap(
                        enumConstant -> {
                            try {
                                Method getCodeMethod = enumClass.getMethod("getCode");
                                return getCodeMethod.invoke(enumConstant);
                            } catch (Exception e) {
                                throw new RuntimeException("枚举类 " + enumClass.getSimpleName() + " 没有 getCode 方法", e);
                            }
                        },
                        enumConstant -> {
                            try {
                                Method getDescMethod = enumClass.getMethod("getDesc");
                                return (String) getDescMethod.invoke(enumConstant);
                            } catch (Exception e) {
                                return enumConstant.name();
                            }
                        }
                ));
    }
    /**
     * 根据枚举类和code值获取枚举实例
     */
    @Override
    public <T extends Enum<T>> T getEnumByCode(Class<T> enumClass, Object code) {
        try {
            Method getCodeMethod = enumClass.getMethod("getCode");
            System.out.println(Arrays.stream(enumClass.getEnumConstants())
                    .filter(e -> {
                        try {
                            Object enumCode = getCodeMethod.invoke(e);
                            return enumCode.equals(code);
                        } catch (Exception ex) {
                            return false;
                        }
                    })
                    .findFirst()
                    .orElse(null));
            return Arrays.stream(enumClass.getEnumConstants())
                    .filter(e -> {
                        try {
                            Object enumCode = getCodeMethod.invoke(e);
                            return enumCode.equals(code);
                        } catch (Exception ex) {
                            return false;
                        }
                    })
                    .findFirst()
                    .orElse(null);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("枚举类 " + enumClass.getSimpleName() + " 没有 getCode 方法", e);
        }
    }
    /**
     * 根据枚举类和name获取枚举实例
     */
    @Override
    public <T extends Enum<T>> T getEnumByName(Class<T> enumClass, String name) {
        System.out.println(Arrays.stream(enumClass.getEnumConstants())
                .filter(e -> e.name().equals(name))
                .findFirst()
                .orElse(null));
        return Arrays.stream(enumClass.getEnumConstants())
                .filter(e -> e.name().equals(name))
                .findFirst()
                .orElse(null);
    }
    /**
     * 根据条件过滤枚举值
     */

    @Override
    public <T extends Enum<T>> List<T> getEnumsByCondition(Class<T> enumClass, Predicate<T> condition) {
       System.out.println(Arrays.stream(enumClass.getEnumConstants())
               .filter(condition)
               .collect(Collectors.toList()));
        return Arrays.stream(enumClass.getEnumConstants())
                .filter(condition)
                .collect(Collectors.toList());
    }

    /**
     * 获取枚举的描述信息（如果有getDesc方法）
     */
    public <T extends Enum<T>> String getEnumDescription(T enumInstance) {
        try {
            Method getDescMethod = enumInstance.getClass().getMethod("getDesc");
            return (String) getDescMethod.invoke(enumInstance);
        } catch (Exception e) {
            return enumInstance.name();
        }
    }

    /**
     * 根据描述信息获取枚举
     */
    public <T extends Enum<T>> T getEnumByDescription(Class<T> enumClass, String description) {
        try {
            Method getDescMethod = enumClass.getMethod("getDesc");
            return Arrays.stream(enumClass.getEnumConstants())
                    .filter(e -> {
                        try {
                            String desc = (String) getDescMethod.invoke(e);
                            return desc.equals(description);
                        } catch (Exception ex) {
                            return false;
                        }
                    })
                    .findFirst()
                    .orElse(null);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }
}